package cn.js.sip.rat.system.controller;

import cn.js.sip.rat.system.dto.AjaxResult;
import cn.js.sip.rat.system.entity.SysUserEntity;
import cn.js.sip.rat.system.entity.ZtreeEntity;
import cn.js.sip.rat.system.page.TableDataInfo;
import cn.js.sip.rat.system.service.SysRoleService;
import cn.js.sip.rat.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: cn.js.sip.rat.system.controller
 * @Description: TODO
 * @author: xuxiaoli
 * @Date: 2020-01-03   20:59
 */
@Api("用户管理")
@Controller
@RequestMapping("system/user")
public class SysUserController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(SysUserController.class);
    private String prefix = "system/user";
    @Resource(name = "sysUserService")
    private SysUserService sysUserService;
    @Resource(name = "sysRoleService")
    private SysRoleService sysRoleService;

    @ApiOperation(value = "view", notes = "跳转至user页面")
    @RequiresPermissions("system:user:view")
    @GetMapping
    public String user() {
        return prefix + "/user";
    }

    @ApiOperation(value = "list",notes = "用户列表")
    @RequiresPermissions("system:user:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUserEntity entity) {
        return sysUserService.query(entity);
    }


    @GetMapping("/add")
    public String toAdd(Map<String, Object> model) {

        model.put("roles", sysRoleService.getAll());
        return prefix + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(SysUserEntity sysMenuEntity) {
        sysUserService.update(sysMenuEntity);
        return AjaxResult.success();
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") String userId, ModelMap mmap) {
        mmap.put("user", sysUserService.findById(userId));
        mmap.put("roles", sysRoleService.getRolesByUserId(userId));
        return prefix + "/edit";
    }

    /* *//**
     * 修改保存用户
     *//*
    @RequiresPermissions("system:user:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysUserEntity user) {
        userService.checkUserAllowed(user);
        if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(sysUserService.checkPhoneUnique(user))) {
            return error("修改用户'" + user.getLoginName() + "'失败，手机号码已存在");
        } else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            return error("修改用户'" + user.getLoginName() + "'失败，邮箱账号已存在");
        }
        user.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(sysUserService.updateUser(user));
    }*/

    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<ZtreeEntity> treeData() {
        List<ZtreeEntity> ztrees = new ArrayList<>();
        return ztrees;
    }

    /**
     * 校验用户名
     */
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(SysUserEntity user) {
        return sysUserService.checkLoginNameUnique(user.getAccount());
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(SysUserEntity user) {
        return sysUserService.checkPhoneUnique(user);
    }

    /**
     * 校验email邮箱
     */
    @PostMapping("/checkEmailUnique")
    @ResponseBody
    public String checkEmailUnique(SysUserEntity user) {
        return sysUserService.checkEmailUnique(user);
    }
}
